# Contributor: Laurent Bercot <ska-devel@skarnet.org>
# Maintainer: Laurent Bercot <ska-devel@skarnet.org>
pkgname=tipidee
pkgver=0.0.4.0
pkgrel=1
pkgdesc="The skarnet.org web server"
url=https://skarnet.org/software/tipidee/
arch="all"
options="!check"  # No test suite.
license="ISC"
depends="execline s6 s6-networking"
makedepends="skalibs-dev>=2.14"
subpackages="$pkgname-static $pkgname-dev $pkgname-libs $pkgname-doc $pkgname-openrc"
source="
	https://skarnet.org/software/tipidee/tipidee-$pkgver.tar.gz
	httpd.run
	httpsd.run
	logger.run
	tipidee.conf
	tipidee.confd
	tipidee.initd
	"
install="
	tipidee.pre-install
	tipidee.post-install
	tipidee.post-deinstall
	"
pkggroups="tipidee"
pkgusers="tipideelog tipideed"

build() {
	./configure \
		--enable-shared \
		--enable-static \
		--disable-allstatic \
		--prefix=/usr \
		--sysconfdir=/etc \
		--libdir=/usr/lib \
		--libexecdir="/usr/lib/$pkgname" \
		--with-dynlib=/lib
	make
}

_makeservicedir() {
	name="http${1}d-${2}"
	fn="$pkgdir/var/lib/tipidee/services/$name"
	logdir="$pkgdir/var/log/$name"
	mkdir -p "$fn/log" "$logdir"
	chown tipideelog:tipidee "$logdir"
	chmod 02755 "$logdir"
	echo 3 > "$fn/log/notification-fd"
	sed -e "s/@S@/${1}/g; s/@V@/${2}/g;" "$srcdir/logger.run" > "$fn/log/run"
	chmod 0755 "$fn/log/run"
	echo 3 > "$fn/notification-fd"
	sed -e "s/@V@/${2}/g;" "$srcdir/http${1}d.run" > "$fn/run"
	chmod 0755 "$fn/run"
}

package() {
	mkdir -p "$pkgdir/usr/share/doc" "$pkgdir/var/lib/tipidee/docroot"
	make DESTDIR="$pkgdir" install
	for i in '' s ; do for j in 4 6 ; do
		_makeservicedir "$i" "$j"
	done ; done
	install -D -m 0644 "$srcdir/tipidee.conf" "$pkgdir/etc/tipidee.conf"
	install -D -m 0644 "$srcdir/tipidee.confd" "$pkgdir/etc/conf.d/tipidee"
	install -D -m 0755 "$srcdir/tipidee.initd" "$pkgdir/etc/init.d/tipidee"
	cp -a "$builddir/doc" "$pkgdir/usr/share/doc/$pkgname"
}

dev() {
	default_dev

	# default_dev please stop being stupid
	mv "$subpkgdir/usr/bin/tipidee-config" "$pkgdir/usr/bin/tipidee-config"
}

sha512sums="
d177f4c00db9855792dbddc4c96d2c75bd302443f29376e1d35d511d56964e5de11dd2cf4af3190d7465404c429eedfc17264715e4850d6b830a9e492ff25741  tipidee-0.0.4.0.tar.gz
0de0d6bd39adfd6e65694335e9cf06cec7625032e6af5cf5e54ced5c009707ca4e27b2dbdff8b7825575fbef7cd98ac0333e07b1924f24fda23019c92c08023f  httpd.run
65cdbfb5a6485aed0203ed910af124b202994d5253cb365ed25432f0c906471dd30f96eb862b6f25004c6ec8ec4cb38a5b49cde717e7a00d7c9d7fd07025f225  httpsd.run
b5af306fe7040f966b13da84bfb0c4b528683c03205519d08fe429ee826ffad27c9ddb32be8c8bf8b42ce7964ef15572e3c66a96494aed833ee951c769f81bd2  logger.run
bb2681a3000133724bb8fd4918bc3209ea676c2e4f96ad8b298d8e7302aeebd6876341ea205f2d808f34e4c9b5c138bba032694c6069c17026b0ddf23702f9ba  tipidee.conf
7a999200ad21b2b0737ce82136bc8e055d7ab4a865b7727e6dc8df0eaeea6ace11eb5756b64231ce7938d11b5ec218944173ea1df3db059d033bf3f848c9a608  tipidee.confd
9e0c8ed97894c60dc37b38debf9e087228c259e08bb6a17b35c756bcaa57903d3608620eb26aba6295c0fb087e4ab4c551bc65acd37d8b92b7b9e1c911542dd1  tipidee.initd
"
